package algorithm;

import DataStructure.CharStack;
import DataStructure.Tree;
import DataStructure.TreeNode;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import util.Polynomial;
import util.Symbol;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Random;

public class CPABECT {
    private Tree tree;
    private Element cm;
    //    private BigInteger cm;
    private Element C;
    private EAUnion[] cy1;
    private EAUnion[] cy2;
    private String policy;
    private Pairing pairing;


    public CPABECT() {
    }

    public CPABECT(Tree tree, Element cm, Element c, EAUnion[] cy1, EAUnion[] cy2, String policy) {
        this.tree = tree;
        this.cm = cm;
        C = c;
        this.cy1 = cy1;
        this.cy2 = cy2;
        this.policy = policy;
    }

    public Tree getTree() {
        return tree;
    }

    public Element getCm() {
        return cm;
    }

    public Element getC() {
        return C;
    }

    public EAUnion[] getCy1() {
        return cy1;
    }

    public EAUnion[] getCy2() {
        return cy2;
    }

    public String[] toStringArray() {
        String[] array = new String[5];
        array[0] = Base64.getEncoder().encodeToString(cm.toBytes());
        array[1] = Base64.getEncoder().encodeToString(C.toBytes());
        StringBuilder builder1 = new StringBuilder();
        for (int i = 0; i < cy1.length; i++) {
            builder1.append(cy1[i].toString());
            //数组分界符
            builder1.append("%");
        }
        array[2] = builder1.toString();
        StringBuilder builder2 = new StringBuilder();
        for (int i = 0; i < cy2.length; i++) {
            builder2.append(cy2[i].toString());
            //数组分界符
            builder2.append("%");
        }
        array[3] = builder2.toString();
        array[4] = policy;
        return array;
    }

    public void fromStringArray(String[] input, Pairing pairing1) throws Exception {
        this.pairing = pairing1;
        Field G1 = pairing.getG1();
        Field GT = pairing.getGT();
        cm = GT.newElement();
        cm.setFromBytes(Base64.getDecoder().decode(input[0]));
        cm = cm.getImmutable();
        C = G1.newElement();
        C.setFromBytes(Base64.getDecoder().decode(input[1]));
        C = C.getImmutable();
        //获得各个数据的组
        String[] arrayCY1 = input[2].split("%");
        cy1 = new EAUnion[arrayCY1.length];
        for (int i = 0; i < arrayCY1.length; i++) {
            cy1[i] = new EAUnion();
            cy1[i].fromString(arrayCY1[i], G1);
        }
        String[] arrayCY2 = input[3].split("%");
        cy2 = new EAUnion[arrayCY2.length];
        for (int i = 0; i < arrayCY2.length; i++) {
            cy2[i] = new EAUnion();
            cy2[i].fromString(arrayCY2[i], G1);
        }
        policy = input[4];

    }

    public String getPolicy() {
        return policy;
    }

    public void setTree(Tree tree) {
        this.tree = tree;
    }
}

